#include "stdafx.h"
#include "pyhgeVertex.h"

PyTypeObject VertexType=
{
	PyObject_HEAD_INIT(NULL)
	0,                         /*ob_size*/
	"hge.Vertex",             /*tp_name*/
	sizeof(pyVertex), /*tp_basicsize*/
	0,                         /*tp_itemsize*/
	0,                         /*tp_dealloc*/
	0,                         /*tp_print*/
	0,                         /*tp_getattr*/
	0,                         /*tp_setattr*/
	0,                         /*tp_compare*/
	0,                         /*tp_repr*/
	0,                         /*tp_as_number*/
	0,                         /*tp_as_sequence*/
	0,                         /*tp_as_mapping*/
	0,                         /*tp_hash */
	0,                         /*tp_call*/
	0,                         /*tp_str*/
	0,                         /*tp_getattro*/
	0,                         /*tp_setattro*/
	0,                         /*tp_as_buffer*/
	Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,        /*tp_flags*/
	"Vertex(x,y,z=0,col=0,tx=0,ty=0)\n"
	"The hgeVertex structure is used to describe vertices of which HGE graphic primitives consist.",/* tp_doc */
};
static PyMemberDef Vertex_members[] = {
	{"x",T_FLOAT,offsetof(pyVertex,v.x),0,"Screen X-coordinate."},
	{"y",T_FLOAT,offsetof(pyVertex,v.y),0,"Screen Y-coordinate."},
	{"z",T_FLOAT,offsetof(pyVertex,v.z),0,"Vertex Z-order ranging from 0.0 to 1.0.\nIf Z-buffer isn't used, the value is ignored."},
	{"col",T_UINT,offsetof(pyVertex,v.col),0,"Vertex color and opacity."},
	{"tx",T_FLOAT,offsetof(pyVertex,v.tx),0,"Texture X-coordinate in range 0.0 to 1.0."},
	{"ty",T_FLOAT,offsetof(pyVertex,v.ty),0,"Texture Y-coordinate in range 0.0 to 1.0."},
	{NULL}  /* Sentinel */
};

static PyObject *
Vertex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	//if (type != &VertexType)
	//{
	//	PyErr_SetString(PyExc_TypeError,"Error type in Vertex_new.");
	//	return 0;
	//}
	pyVertex * self = (pyVertex*)type->tp_alloc(type, 0);
	memset(&(self->v),0,sizeof(self->v));
	return (PyObject*)self;
}
static int
Vertex_init(PyObject *self, PyObject *args, PyObject *kwds)
{
	pyVertex * obj = (pyVertex*)self;
	hgeVertex * pv = &(obj->v);
	static char *kwlist[] = {"x", "y", "z", "col","tx","ty"};
	if (! PyArg_ParseTupleAndKeywords(args, kwds, "|fffIff", kwlist, 
		&(pv->x),&(pv->y),&(pv->z),&(pv->z),&(pv->col),&(pv->tx),&(pv->ty)))
		return -1; 
	return 0;
}
static PyObject *
Vertex_repr(PyObject * self)
{
	if (!PyObject_TypeCheck(self,&VertexType))
	{
		PyErr_SetString(PyExc_TypeError,"Vertex is required.");
		return 0;
	}
	pyVertex * obj = (pyVertex*)self;
	hgeVertex * pv = &(obj->v);
	char buf[1024];
	sprintf(buf,"<Vertex x=%.2f y=%.2f>",pv->x,pv->y);
	return PyString_FromString(buf);
}

int
pyInitVertex(PyObject * mod)
{
	VertexType.tp_new = Vertex_new;
	VertexType.tp_init = Vertex_init;
	VertexType.tp_repr = Vertex_repr;
	VertexType.tp_str = Vertex_repr;
	VertexType.tp_members = Vertex_members;
	if (PyType_Ready(&VertexType) < 0) return -1;
	Py_INCREF(&VertexType);
	return PyModule_AddObject(mod, "Vertex", (PyObject *)&VertexType); 
}